package com.mingmen.canting.caigourenyuan;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.PeiSongDetailModel;
import com.mingmen.canting.model.ShopListModel;
import com.mingmen.canting.model.TuiHuoModel;

import java.util.List;


public class QueRenAdapter extends RecyclerView.Adapter<QueRenAdapter.ViewHolder> {
    List<PeiSongDetailModel.DataBean.DetailsBean> myLiveList;
    Context mContext;
    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<PeiSongDetailModel.DataBean.DetailsBean> myLiveList);
    }


    public QueRenAdapter(Context context, List<PeiSongDetailModel.DataBean.DetailsBean> list) {
        this.mContext = context;
        this.myLiveList = list;

    }

    public void setDataList(List<PeiSongDetailModel.DataBean.DetailsBean> list) {
        myLiveList = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_querenshou, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        PeiSongDetailModel.DataBean.DetailsBean detailsBean = myLiveList.get(position);
        holder.checkbox.setChecked(myLiveList.get(position).isSelect());
        Glide.with(mContext).load(AppConfig.IMGURL + detailsBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);

        holder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                myLive.setChecked(!myLive.isChecked());
                myLiveList.get(position).setSelect(!myLiveList.get(position).isSelect());
                holder.checkbox.setChecked(myLiveList.get(position).isSelect());
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });


        holder.mTvTitle.setText(myLiveList.get(position).getCommodityName());

        holder.specs.setText("计划数量: " + myLiveList.get(position).getAmount() + "");
//        holder.confirmAmount.setText("实发数量: " + myLiveList.get(position).getConfirmPrice() + "");
        holder.arriveAmount.setText(myLiveList.get(position).getArriveAmount() + "");
        holder.price.setText("报价:" + myLiveList.get(position).getPrice() + "元");
        holder.applyAmount.setText("实发数量: " + myLiveList.get(position).getApplyAmount() + "");
//        订单单价
        double applyPrice = myLiveList.get(position).getApplyPrice();
//        实际到货单价
        double arrivePrice = myLiveList.get(position).getArrivePrice();

        if (arrivePrice > applyPrice) {
//            holder.text_shiti.setTextColor(Color.parseColor("#BA0017"));
//            holder.text_arrow.setImageResource(R.mipmap.arrow_top);
        } else {
//            holder.text_shiti.setTextColor(Color.parseColor("#2A9C30"));
//            holder.text_arrow.setImageResource(R.mipmap.arrow_blue);
        }

        holder.text_shiti.setText("实价: " + myLiveList.get(position).getConfirmPrice() + "");
        holder.tvSpec.setText("规格: "+myLiveList.get(position).getSpecs());
        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });

        holder.layout_confirmCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });

    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {


        TextView mTvTitle;

        ImageView ivAvatar;


        CheckBox checkbox;
        LinearLayout content;


        LinearLayout layout_guige;
        TextView text_shiti;

        ImageView text_arrow;
        TextView arriveAmount;
        //        TextView amount;
        TextView price;

        TextView specs;

        LinearLayout layout_confirmCount;
        TextView applyAmount;

        TextView tvSpec;
        public ViewHolder(View itemView) {
            super(itemView);

            mTvTitle = (TextView) itemView.findViewById(R.id.tvCity);
            ivAvatar = (ImageView) itemView.findViewById(R.id.ivAvatar);


            checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
            content = (LinearLayout) itemView.findViewById(R.id.content);


            layout_guige = itemView.findViewById(R.id.layout_guige);
            text_shiti = itemView.findViewById(R.id.text_shiti);
            text_arrow = itemView.findViewById(R.id.text_arrow);
            arriveAmount = itemView.findViewById(R.id.arriveAmount);
            applyAmount= itemView.findViewById(R.id.applyAmount);
            price = itemView.findViewById(R.id.price);
            specs = itemView.findViewById(R.id.specs);
            tvSpec= itemView.findViewById(R.id.tvSpec);
            layout_confirmCount = itemView.findViewById(R.id.layout_confirmCount);
//            amount = itemView.findViewById(R.id.amount);
        }
    }


}
